<?php
namespace app\shop\library\order;

use app\shop\library\Member;
use cubox\Cubox;
class MemberOrder{
    public static function count(){
        $shop = Cubox::app('shop');
        $cache = $shop->cache();
        $cachekey = Member::cacheName('order_count');

        if ($cache->exists($cachekey)){
            $re = json_decode($cache->get($cachekey)??'{}',true);
        }else{
            $data = $shop->model('Order')->where('member_id',Member::id())->where('status','in',['new','pay','delivery'])->group('status')->field('status,count(1) as count')->getData();
            $re = [
                'pay'=>0,
                'delivery'=>0,
            ];
            foreach ($data as $key => $val){

                if ($val['status'] == 'new'  || $val['status'] == 'pay'){
                    $re['pay'] += $val['count'];
                }

                if ($val['status'] == 'delivery'){
                    $re['delivery'] += $val['count'];
                }
            }

            $cache->set($cachekey,json_encode($re));
            $cache->expire($cachekey,60);
        }

        return $re;
    }
}